﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.Web.CentralModels;
using Guline.Web.CentralModels.Object;
using Guline.Web.Central.Controllers;
using Guline.PaymentAPI;
using Newtonsoft.Json.Linq;
using System.Data;

namespace Guline.Web.Central.Areas.App.Controllers
{
    public class ApplicationsController : BaseController
    {
        //
        // GET: /App/Applications/

        public ActionResult Index()
        {
            return View(db.CApps.Where(m => m.AccountID == db.Accounts.Where(n => n.UserName == User.Identity.Name).FirstOrDefault().ID).OrderByDescending(m=>m.ID).ToList());
        }
        public ActionResult CreateApp()
        {
            //Account acc = new Account();
            //ViewBag.AccountID = new SelectList(this.db.Accounts, "ID", "UserName");
            return View();
        }
        [HttpPost]
        public ActionResult CreateApp(CApp model)
        {
            if(ModelState.IsValid)
            {
                //CApp App = new CApp();
                model.AccountID = db.Accounts.Where(m => m.UserName == User.Identity.Name).FirstOrDefault().ID;
                this.db.CApps.Add(model);
                this.db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult EidtApp(int ID)
        {
            var app = db.CApps.Where(m => m.ID == ID).FirstOrDefault();
            ViewBag.AccountID = new SelectList(this.db.Accounts, "ID", "UserName", selectedValue: app.AccountID);
            return View(app);
        }
        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult EidtApp(CApp model)
        {
           
           
            if (ModelState.IsValid)
            {
                db.Entry(model).State = EntityState.Modified;//loi cho nay
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            //var app = db.CApps.Where(m => m.ID == model.ID).FirstOrDefault();
            //ViewBag.AccountID = new SelectList(this.db.Accounts, "ID", "UserName",  app.AccountID);
            return View();
        }
        public ActionResult DeleteApp(int ID)
        {
            var app = db.CApps.Where(m => m.ID == ID).FirstOrDefault();
            db.CApps.Remove(app);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}
